<?php
	/*
	 * update categories tree display order.
	 *
	*/
	
	
	error_reporting(E_ALL);
	require_once('config/constant.php');
	require_once(DAEMONE_HOME.'/config/amazon.php');
	require_once(DAEMONE_HOME.'/lib/lib_database.php');
	
	
	
	
	
	
//	$result = execute_query('drop table if exists parent_categories_temp');
//	$result = execute_query('create table parent_categories_temp (category_id int(11), tree_order int(11), level int(2), tree_start int(11), tree_end int(11))');
	
	$tree_order = 0;

	$result1 = execute_query('select category_id from categories where parent_category_id = 0 order by category_name');

	if($result1 === FALSE) 
	{
		echo  mysql_error();
	}
	
	
	while($row1 = mysql_fetch_array($result1, MYSQL_ASSOC))
	{
		$tree_order = $tree_order + 1;
		$tree_start_level_1 = $tree_order;
		$result2 = execute_query('select category_id from categories where parent_category_id = '.$row1['category_id'].' order by category_name');
		
		while($row2 = mysql_fetch_array($result2, MYSQL_ASSOC))
		{
			
			$tree_order = $tree_order + 1;
			$tree_start_level_2 = $tree_order;

			echo 'category_id=', $row2['category_id'], ' level 2', "\r\n";
			//echo 'update categories set tree_order='.$tree_start_level_2.', tree_start='.$tree_start_level_2.', tree_end='.$tree_order.' where category_id='.$row2['category_id'], "\r\n";
			execute_query('update categories set tree_order='.$tree_start_level_2.', tree_start='.$tree_start_level_2.', tree_end='.$tree_order.' where category_id='.$row2['category_id']);
			if($result1 === FALSE) 
			{
				echo  mysql_error();
			}
		}
		
		echo 'category_id=', $row1['category_id'], ' level 1', "\r\n";
		execute_query('update categories set tree_order='.$tree_start_level_1.', tree_start='.$tree_start_level_1.', tree_end='.$tree_order.' where category_id='.$row1['category_id']);
	}

	exit;
?>
